package com.hzdl.house_mgr.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.house_mgr.bean.Menu;
import com.hzdl.house_mgr.bean.Role;
import com.hzdl.house_mgr.service.MenuService;
import com.hzdl.house_mgr.service.RoleService;
import com.hzdl.house_mgr.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("sysRole")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @RequestMapping("role/roleRightJson")
    public AjaxResult roleRightJson(String roleName, String roleDesc, Integer page, Integer limit){
        PageInfo<Role> pageInfo = roleService.findRoleByWhere(roleName,roleDesc, page, limit);
        Integer count = roleService.getCountByWhere(roleName,roleDesc);
        return AjaxResult.ok(count,pageInfo.getList());
    }

    @RequestMapping("role/roleAddOrUpdate")
    public AjaxResult roleAddOrUpdate(Role role){
        if(role.getRoleId() == null){
            int i = roleService.insert(role);
            if(i>0){
                return AjaxResult.ADD_SUCCESS();
            }else{
                return AjaxResult.ADD_FAILED();
            }
        }else{
            int i = roleService.updateRole(role);
            if(i>0){
                return AjaxResult.UPD_SUCCESS();
            }else{
                return AjaxResult.UPD_FAILED();
            }
        }
    }

    @RequestMapping("role/roleLeftJson")
    public AjaxResult roleLeftJson(Integer roleId){
        //所有的菜单
        List<Menu> all = menuService.findAll();

        //当前角色拥有的菜单
        List<Menu> menuByRoleId = menuService.findMenuByRoleId(roleId);

        /*方法一*/
        /*for(Menu m: all){
            //获取当前菜单m
            Integer id = m.getId();
            //判断该角色是否有改菜单
            Boolean flag = false;
            for (Menu m2: menuByRoleId){
                if(id == m2.getId()){
                    flag = true;
                    break;
                }
            }
            if(flag){
                m.setCheckArr("1");
            }
        }*/
        for(Menu m: all){
            for (Menu m2: menuByRoleId){
                if(m.getId() == m2.getId()){
                    m.setCheckArr("1");
                    break;
                }
            }
        }
        return AjaxResult.ok(all);
    }

    @RequestMapping("role/allotMenuByRoleId")
    public String allotMenuByRoleId(Integer roleId,String roleIds){
        String[] split = roleIds.split(",");
        Integer[] arr = new Integer[split.length];
        for(int i = 0;i<arr.length;i++){
            arr[i] = Integer.valueOf(split[i]);
        }
        int i = roleService.updRoleMenuByRoleId(roleId,arr);
        if(i>0){
            return "1";
        }else{
            return "";
        }
    }

    @RequestMapping("role/roleDelete")
    public AjaxResult roleDelete(Integer id){
        int i = roleService.deleteRoleById(id);
        if(i>0){
            return AjaxResult.DEL_SUCCESS();
        }else{
            return AjaxResult.DEL_FAILED();
        }
    }
}
